//
//  RootViewController.m
//  XRLoopSegmentControl
//
//  Created by TQS on 15/4/11.
//  Copyright (c) 2015年 寒竹子. All rights reserved.
//

#define ScreenSize [UIScreen mainScreen].bounds.size

#import "XRSegmentNavigation.h"
#import "XRSegmentControl.h"

@interface XRSegmentNavigation()<XRSegmentControlDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) XRSegmentControl * segment;
@property (nonatomic, strong) UIScrollView * bottomScrollView;
@property (nonatomic, strong) NSMutableArray * viewControllers;
@property (nonatomic, strong) NSArray * titles;

@end

@implementation XRSegmentNavigation

- (instancetype)initWithViewControllers:(NSMutableArray *)viewControllers titles:(NSArray *)titles callBlock:(CallBackBlock)myBlock
{
    if (self = [super init]) {
        self.myBlock = [myBlock copy];
        _titles = [titles copy];
        _viewControllers = [viewControllers mutableCopy];
        self.items = viewControllers;
    }
    return self;
}

/**
 * @brief   初始化UI
 *
 * @param
 * @return
 */
- (void)setupUI
{
    [self.view addSubview:self.segment];
    [self.view addSubview:self.bottomScrollView];
}

/**
 * @brief   segmentControl
 *
 * @param
 * @return
 */
- (XRSegmentControl *)segment
{
    if (nil == _segment) {
        _segment = [[XRSegmentControl alloc]
                    initWithFrame:CGRectMake(0, 0, ScreenSize.width, _barHeight)
                                                    titles:_titles
                                                  delegate:self
                                                  textFont:_textFont
                                           normalTextColor:_normalColor
                                             selectedColor:_selectedColor
                                                 barHeight:_barHeight
                                                     image:_image
                                           backgroundColor:_backgroundColor
                                              defaultIndex:_defaultIndex];
    }
    
    return _segment;
}

/**
 * @brief   bottomScrollView
 *
 * @param
 * @return
 */
- (UIScrollView *)bottomScrollView
{
    if (nil == _bottomScrollView) {
        _bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_segment.frame), ScreenSize.width, ScreenSize.height - CGRectGetMaxY(_segment.frame))];
        _bottomScrollView.delegate = self;
        _bottomScrollView.pagingEnabled = YES;
        _bottomScrollView.bounces = NO;
        _bottomScrollView.scrollEnabled = NO;
        _bottomScrollView.showsVerticalScrollIndicator = NO;
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
        _bottomScrollView.contentSize = CGSizeMake(ScreenSize.width * _viewControllers.count, 0);
        
        // 添加ViewController
        for (int i = 0; i < _viewControllers.count; i++) {
            ((UIViewController *)_viewControllers[i]).view.frame = CGRectMake(i * ScreenSize.width, 0, ScreenSize.width, _bottomScrollView.bounds.size.height);
            [_bottomScrollView addSubview:((UIViewController *)_viewControllers[i]).view];
        }
        
        _bottomScrollView.contentOffset = CGPointMake(ScreenSize.width*self.defaultIndex, 0);
    }
    
    return _bottomScrollView;
}

#pragma mark - XRSegmentControlDelegate
- (void)segmentSelectedIndexChanged:(NSInteger)selectedIndex
{
    [self.bottomScrollView setContentOffset:CGPointMake(ScreenSize.width * selectedIndex, 0) animated:NO];
    
    if (self.myBlock) {
        self.myBlock(selectedIndex);
    }
}

#pragma mark - ScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / ScreenSize.width;
    
//    // 通知改变segment的selectedIndex
    [[NSNotificationCenter defaultCenter] postNotificationName:SelectedIndexChanged_Notification object:[NSNumber numberWithInteger:index]];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupUI];
}

@end
